﻿using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    /// <summary>
    /// 咨询访问数据层
    /// </summary>
    public class ConsultationDAL
    {
        private readonly Database Database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        /// <summary>
        /// 增加咨询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="telephone"></param>
        /// <param name="delegateType"></param>
        /// <param name="remark"></param>
        /// <param name="qq"></param>
        /// <param name="empolyeeID"></param>
        /// <returns></returns>
        public bool AddConsultation(string name, string telephone, int delegateType, string remark, string qq,int empolyeeID)
        {
            const string sql = @"insert into consultation (ConsultativeDate,CustomerName,EmployeeID,DelegateType,QQ,Telephone,Remark) 
                            values (GETDATE(),@CustomerName,@EmployeeID,@DelegateType,@QQ,@Telephone,@Remark)";

            using (var cmd = Database.GetSqlStringCommand(sql))
            {
                Database.AddInParameter(cmd, "@CustomerName", DbType.String, name);
                Database.AddInParameter(cmd, "@EmployeeID", DbType.Int32, empolyeeID);
                Database.AddInParameter(cmd, "@DelegateType", DbType.Int32, delegateType);
                Database.AddInParameter(cmd, "@QQ", DbType.String, qq);
                Database.AddInParameter(cmd, "@Telephone", DbType.String, telephone);
                Database.AddInParameter(cmd, "@Remark", DbType.String, remark);

                return Database.ExecuteNonQuery(cmd) == 1;
            }
        }

        /// <summary>
        /// 标注咨询无效
        /// </summary>
        /// <param name="consultationID"></param>
        /// <returns></returns>
        public bool RemoveConsultation(int consultationID)
        {
            const string sql = "update Consultation set [status]=1 where ConsultationID=@consultationid";

            using (var cmd = Database.GetSqlStringCommand(sql))
            {
                Database.AddInParameter(cmd, "@consultationid",DbType.Int32,consultationID);

                return Database.ExecuteNonQuery(cmd) == 1;
            }
        }
    }

}
